/**
 * Copyright (C) 2005 WUZEWEN. All rights reserved.
 * WZW PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.kingcore.pda.database.controller;

                 
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kingcore.pda.core.util.ServletUtils;
import com.kingcore.pda.file.bean.FileBean;
 

/**
 * <p>Description: oracle/sqlserver2000数据库表信息生成 *.woo.table 文件到目录(可配置)："E:/ProgramXP/test/" </p>
 * <p>Copyright: Copyright (c) 2005 WUZEWEN</p>
 * @author	WUZEWEN on 2005-7-27
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK5
 */


public class CodeFileAction extends TableFileAction {
	
    public ActionForward executeAction(ActionMapping mapping, ActionForm form, 
    									HttpServletRequest request, 
    									HttpServletResponse response)
								throws ServletException, Exception{
    	
		String action = request.getParameter("action"); // wzw:action好于Action

		if (action == null || "".equals(action.trim())) { // 创建表文件的默认行为
    		return this.createCodeFile( mapping, form, request, response);

		} else { // 如果action不为空，反射调用对应方法
			System.out.println("开始调用Action方法：" + action);
			// JavaBean内部属性和ResultSet中一致时候
			Method setMethod = this.getClass().getMethod(action,
					ActionMapping.class, ActionForm.class,
					HttpServletRequest.class, HttpServletResponse.class);

			return (ActionForward) setMethod.invoke(this, 
					mapping, form,
					request, response);

		}
	}

	/**
	 * 创建表文件。 该类要求线程安全。
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
    public synchronized ActionForward createCodeFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

////		var url = '/pda/createCodeFile.wsp?tblName=${param.tblName}';
////		  url+='&columns=${columns}'
////		  url+='&moduleName='+$("#moduleName").val()
////		  url+='&jetfile='+file;
//
//		String tblName = request.getParameter("tblName");
//		String columns = request.getParameter("columns");
//		String moduleName = ServletUtils.getParameterTranscoded(request,"moduleName");
//		String moduleLabel = ServletUtils.getParameterTranscoded(request,"moduleLabel");
//		String className = ServletUtils.getParameterTranscoded(request,"className");
//		String jetfile = ServletUtils.getParameterTranscoded(request,"jetfile");	
//		String pkg = request.getParameter("package");
//		String pkgFolder = request.getParameter("packageFolder");
//		String parentFolder = "";
//		//full package
//		if (pkg!=null&&pkg.endsWith(".")) {
//			pkg = pkg.substring(0, pkg.length()-1);
//		}
//
//		//parent package
//		if (pkgFolder!=null&&pkgFolder.endsWith(".")) {
//			pkgFolder = pkgFolder.substring(0, pkgFolder.length()-1);
//		}
//		if (pkgFolder!=null) {
//			parentFolder = pkgFolder.substring( pkgFolder.lastIndexOf(".")+1);	
//		}
//		
//		ModuleBean moduleBean = new ModuleBean();
//		data.put("module", moduleBean);	
//		moduleBean.setName(moduleName);
//		moduleBean.setLabel(moduleLabel);
//		moduleBean.setComment(moduleLabel);
//		moduleBean.setClassName(className);
//		moduleBean.setPackagee(pkg); 
//		moduleBean.setFolder(parentFolder); 
//
//		log.info("pkg="+pkg+ "  parentFolder="+parentFolder+ "  moduleName="+moduleName+" jetfile:"+jetfile);
//		
//		this.createTableFile(mapping, request, response);
//
//		//this.saveError(request);
//		request.setAttribute("data", data);

		String jetfile = ServletUtils.getParameterTranscoded(request,"jetfile");	
		
		
		String tblName = request.getParameter("tblName");
		String columns = request.getParameter("columns");
		String moduleName = ServletUtils.getParameterTranscoded(request,"moduleName");
		String moduleLabel = ServletUtils.getParameterTranscoded(request,"moduleLabel");
		String className = ServletUtils.getParameterTranscoded(request,"className");
//		String jetfile = ServletUtils.getParameterTranscoded(request,"jetfile");	
		String pkg = request.getParameter("package");
		String pkgFolder = request.getParameter("packageFolder");
		String parentFolder = "";
		
		//full package
		if (pkg!=null&&pkg.endsWith(".")) {
			pkg = pkg.substring(0, pkg.length()-1);
		}

		//parent package
		if (pkgFolder!=null&&pkgFolder.endsWith(".")) {
			pkgFolder = pkgFolder.substring(0, pkgFolder.length()-1);
		}
		if (pkgFolder!=null) {
			parentFolder = pkgFolder.substring( pkgFolder.lastIndexOf(".")+1);	
		}
		
		//--------------- 前后都赋值一次
		moduleBean.setName(moduleName);
		moduleBean.setLabel(moduleLabel);
		moduleBean.setComment(moduleLabel);
		moduleBean.setClassName(className);
		moduleBean.setPackagee(pkg); 
		
		if(parentFolder!=null && parentFolder.startsWith("/")){
			parentFolder = parentFolder.substring(1);
		}
		moduleBean.setFolder(parentFolder); 
		this.doCreateCodeFile(mapping, form, request, response);
		//--------------- 前后都赋值一次 end
		
		data.put("module", moduleBean);	
		moduleBean.setName(moduleName);
		moduleBean.setLabel(moduleLabel);
		moduleBean.setComment(moduleLabel);
		moduleBean.setClassName(className);
		moduleBean.setPackagee(pkg); 

		if(parentFolder!=null && parentFolder.startsWith("/")){
			parentFolder = parentFolder.substring(1);
		}
		moduleBean.setFolder(parentFolder); 

		
		FileBean fileBean = new FileBean();
		data.put("file", fileBean);	
		String author = ServletUtils.getParameterTranscoded(request,"author");
		fileBean.setAuthor(author);

		log.info("pkg="+pkg+ "  parentFolder="+parentFolder+ "  className="+className+ " jetfile:"+jetfile); 
		String url = "/template/"+jetfile;   // /pda/...
		request.getRequestDispatcher( url ).forward(request,response);
		return null;
//		return mapping.findForward("success");
		
	}
	
}

